package com.cjm.one.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.annotation.IgnoreTenantForAdmin;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.model.base.BaseDeleteMoreVo;
import com.cjm.one.model.system.dto.SysRoleDto;
import com.cjm.one.model.system.entity.SysRole;
import com.cjm.one.model.system.entity.SysUserPost;
import com.cjm.one.model.system.vo.RoleDetailVO;
import com.cjm.one.system.mapper.SysUserPostMapper;
import com.cjm.one.system.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/")
@Slf4j
@Tag(name = "系统管理-角色")
@IgnoreTenantForAdmin
public class SysRoleContrller {
    @Resource
    private SysRoleService sysRoleService;



    /** 查询角色列表 全部角色 */
    @GetMapping("role/list")
    @Operation(summary = "查询角色列表 全部角色")
    @IgnoreTenantForAdmin
    public Result<List<SysRole>> list() {
        log.info("查询角色列表 全部角色");
        return Result.ok(sysRoleService.getAllRoleList());
    }
    @PostMapping("role/page/list")
    @Operation(summary = "查询角色列表 分页")
    @IgnoreTenantForAdmin
    public Result<Page<SysRole>> page(@RequestBody SysRoleDto sysRoleDto) {
        log.info("查询角色列表 分页:{}", sysRoleDto);
        Page<SysRole> page = new Page<>(sysRoleDto.getCurrent(), sysRoleDto.getSize());
        return Result.ok(sysRoleService.getRolePage(page,sysRoleDto));
    }
    @PutMapping("/role")
    @Operation(summary = "更新角色")
    @IgnoreTenantForAdmin
    public Result<Boolean> update(@RequestBody RoleDetailVO sysRole) {
        log.info("更新角色sysRole:{}", sysRole);
        return Result.ok(sysRoleService.updateRole(sysRole));
    }
    @PostMapping("/role")
    @Operation(summary = "新增角色")
    @IgnoreTenantForAdmin
    public Result<Boolean> addSysRole(@RequestBody RoleDetailVO sysRole) {
        log.info("新增角色sysRole:{}", sysRole);
        return Result.ok(sysRoleService.addSysRole(sysRole));
    }
    @DeleteMapping("/role/{id}")
    @Operation(summary = "删除角色")
    @IgnoreTenantForAdmin
    public Result<Boolean> delete(@PathVariable("id") Long id) {

        log.info("删除角色id:{}", id);

        return Result.ok(sysRoleService.deleteById(id));

    }
    @GetMapping("/role/{id}")
    @Operation(summary = "根据id查询角色")
    @IgnoreTenantForAdmin
    public Result<SysRole> getById(@PathVariable("id") Long id) {
        log.info("根据id查询角色id:{}", id);
        return Result.ok(sysRoleService.getById(id));
    }

    @GetMapping("/role/{id}/detail")
    @Operation(summary = "根据id查询角色详情包含权限列表")
    @IgnoreTenantForAdmin
    public Result<RoleDetailVO> getSysRoleDetail(@PathVariable("id") Long id) {
        log.info("根据id查询角色详情包含权限列表:{}", id);
        return Result.ok(sysRoleService.selectRoleDetail(id));
    }
    @DeleteMapping("role/batch")
    @Operation(summary = "批量删除角色")
    @IgnoreTenantForAdmin
    public Result<BaseDeleteMoreVo> deleteBatchIds(@RequestBody List<Long> ids) {
        log.info("批量删除角色ids:{}", ids);
        return Result.ok(sysRoleService.removeBySysRoleIds(ids));
    }

    @PutMapping("role/status")
    @Operation(summary = "更新角色状态")
    @IgnoreTenantForAdmin
    public Result<Boolean> updateStatus(@RequestBody SysRole sysRole) {
        log.info("更新角色状态:{}", sysRole);
        return Result.ok(sysRoleService.updateStatus(sysRole));
    }


}
